from django import forms
from django.contrib.auth.models import User

from models import Entry, Connection

class FormEntry(forms.ModelForm):
    class Meta:
        model = Entry
        fields = ('content',)

class FormConnection(forms.ModelForm):
    username = forms.CharField(widget=forms.HiddenInput)

    class Meta:
        model = Connection
        fields = ('status',)

class FormFindFriend(forms.Form):
    name = forms.CharField(max_length=35, required=False)
    email = forms.EmailField(max_length=35, required=False)

    def find(self):
        name = self.cleaned_data['name']
        email = self.cleaned_data['email']

        ret = User.objects.filter(username__icontains=name) |\
              User.objects.filter(first_name__icontains=name) |\
              User.objects.filter(last_name__icontains=name) |\
              User.objects.filter(email=email)

        exact_username = ret.filter(username__iexact=name)

        if exact_username.count() == 1:
            return exact_username

        return ret.distinct()

